package org.hnxxxy.ebms.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.net.UnknownHostException;

/**
 * @Description: Redis 配置
 * @Author WANGWEI
 * @Date: 2021/1/21  16:22
 * @EMAIL: 1377366738@qq.com
 */
@Slf4j
@Configuration
public class RedisConfig {

    /**
     * @Description: redisTemplate序列化
     * @Author WANGWEI
     * @Date: 2021/1/21  16:30
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 设置序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();

        template.setKeySerializer(genericFastJsonRedisSerializer);
        template.setHashKeySerializer(genericFastJsonRedisSerializer);
        template.setHashValueSerializer(genericFastJsonRedisSerializer);
        template.setStringSerializer(stringRedisSerializer);
        template.setValueSerializer(genericFastJsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    /**
     * @Description: 配置Jedis 连接池
     * @Author WANGWEI
     * @Date: 2021/1/21  16:30
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean
    public RedisConnectionFactory redisConnectionFactory(){
        log.info("开始设置redis连接池");
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMinIdle(2);
        jedisPoolConfig.setMaxWaitMillis(15 * 1000);
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("82.156.168.244");
        redisStandaloneConfiguration.setPassword("123456");
        return new JedisConnectionFactory(jedisPoolConfig);
    }
}
